import {
  removeModel
} from '../../models/remove';
import page from '../../databus/page';

const remove = ({
  $box,
  url,
  loadData,
  // dataList,
  state
}) => {
  //删除操作事件
  $box.on('click', '.remove', async function () {
    length = state.list.length;
    let result = await removeModel({
      url,
      id: $(this).data('id')
    });

    if (result.ret) {
      // loadData();
      // const isLastPage = Math.ceil(dataList.length / page.pageSize) === page.currentPage;
      // const restOne = dataList.length % page.pageSize === 1;
      // const notPageFirst = page.currentPage > 0;
      // if (isLastPage && restOne && notPageFirst) {
      //   page.setCurrentPage(page.currentPage - 1)
      //   // currentPage--;
      // }


      loadData();
      const isLastPage = Math.ceil(length / page.pageSize) === page.currentPage;
      const restOne = length % page.pageSize === 1;
      const notPageFirst = page.currentPage > 0;
      if (isLastPage && restOne && notPageFirst) {
        page.setCurrentPage(page.currentPage - 1);
      }
    }
  });
}

export {
  remove
}